﻿using PipeDetection.Model;
using PipeDetection.Repository;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PipeDetection.UI.缺陷
{
    public partial class FrmNewDetection : UIForm2
    {
        private DefectCatalog result=new DefectCatalog();
        private bool isedit = false;

        public FrmNewDetection()
        {
            InitializeComponent();
            init1();
        }
        public FrmNewDetection( DefectCatalog result ):this() {
            isedit = true;
            this.result = result;
            Text = "修改缺陷分类";
            Read();
        }
        private void Read()
        {
              txtName.Text= result.DefectCatalogName;
             uiCpLab.Value= result.LabelingTextColor;
             uiCpSym.Value= result.ColorOfPorL;
            uiCmbGeotype.SelectedIndex =(int) result.GeometryType;
            uiCmbpsize.SelectedIndex = (int)result.PointSize;
            uiCmbltype.SelectedIndex = (int)result.LineType;
            uiCmbHtype.SelectedIndex = (int)result.HatchPattern;
            uiIntegerUpDown1.Value = result.LabelFontSize;
            uicmbLineWidth.SelectedIndex = (int)result.LineWidth;
            uiCpHcol.Value = result.HatchColor;
            
            pictureBox2.Image=   result.IconOfButton ;
        }
        private void init1()
        {
            uiCmbGeotype.DataSource = Enum.GetValues(typeof(Gtype));
            uiCmbHtype.DataSource = Enum.GetValues(typeof(Hpattern));
            uiCmbltype.DataSource = Enum.GetValues(typeof(Ltype));
            uiCmbpsize.DataSource = Enum.GetValues(typeof(Psize));
            uicmbLineWidth.DataSource = Enum.GetValues(typeof(Lwidth));
        }
        private void FrmNewDetection_Load(object sender, EventArgs e)
        {
       
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            updateresult();
            if (result.DefectCatalogName == "")
            {
                UIMessageBox.ShowError("名称不能为空！", true, 3);
                return;
            }
            if (isedit)
            {
                Globel.DefectCatalogRepository.Update(result);
            }
            else
                Globel.DefectCatalogRepository.Add(result);
  
                DialogResult = DialogResult.OK;
          
        }
        private void updateresult()
        {
            result.DefectCatalogName = txtName.Text;
            result.LabelingTextColor = uiCpLab.Value;
            result.ColorOfPorL = uiCpSym.Value;
            result.GeometryType = (Gtype)Enum.Parse(typeof(Gtype), uiCmbGeotype.Text);
            result.LineWidth = (Lwidth)Enum.Parse(typeof(Lwidth), uicmbLineWidth.Text);
            switch (result.GeometryType)
            {
                case Gtype.点:
                    result.PointSize = (Psize)Enum.Parse(typeof(Psize), uiCmbpsize.Text);
                    break;
                case Gtype.线:
                    result.LineType = (Ltype)Enum.Parse(typeof(Ltype), uiCmbltype.Text);
                    break;
                case Gtype.面:
                case Gtype.矩形:
                    result.HatchPattern = (Hpattern)Enum.Parse(typeof(Hpattern), uiCmbHtype.Text);
                    result.HatchColor = uiCpHcol.Value;
                    break;
            }
            result.IconOfButton = (Bitmap)pictureBox2.Image;
            result.LabelFontSize = uiIntegerUpDown1.Value;
            
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            updateresult();
            pictureBox1.Image= Common.DrawSymbol(result);
        }

        private void uiCmbGeotype_SelectedIndexChanged(object sender, EventArgs e)
        {
            uiTitlePanel1.Visible = false;
            uiTitlePanel2.Visible = false;
            uiTitlePanel3.Visible = false;
            switch ((Gtype)Enum.Parse(typeof(Gtype), uiCmbGeotype.Text))
            {
                case Gtype.点:
                    uiTitlePanel1.Visible = true;
                    break;
                case Gtype.线:
                    uiTitlePanel2.Visible = true;
                    break;
                case Gtype.面:
                case Gtype.矩形:
                    uiTitlePanel3.Visible = true;
                    break;
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            var fn = "";
            if (FileEx.OpenDialog(ref fn, "图像文件|*.bmp;*.jpg;*.ico"))
            { 
                Stream stream=new MemoryStream(File.ReadAllBytes(fn));
                var img= Image.FromStream(stream);
                Bitmap bmp = new Bitmap(32, 32);
                Graphics g = Graphics.FromImage(bmp);
                g.DrawImage(img, 0, 0, 32, 32);
                g.Dispose();
                pictureBox2.Image=bmp;
            }
        }
    }
}
